//
//  newPayPasswordViewController.m
//  dongfengTest
//
//  Created by wxt on 2021/3/18.
//

#import "NewPayPasswordViewController.h"
#import "ChangeSuccessViewController.h"
@interface NewPayPasswordViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)TKLtextFiled * payWordF;
@property(nonatomic,strong)TKLtextFiled * rePayWordF;
@property(nonatomic,strong)UIButton * okBtn;
@property(nonatomic,strong)UIButton * isHiddenBtn;
@property(nonatomic,strong)TKLlable * payWordMessageLab;
@property(nonatomic,strong)TKLlable * rePayWordMessageLab;
@end

@implementation NewPayPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = Wcolor;
    UIButton * backBtn =[UIButton createBtnFrame:CGRectMake(15, kStatusBarHeight+5, 18, 18) title:@"" bgImageName:@"fanhui" action:^(UIButton *button) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [self.view addSubview:backBtn];
    TKLlable * navTit = [TKLlable labelWithFrame:CGRectMake(kWidth/2-60, kStatusBarHeight+5, 120, 20) text:@"" textColor:Bcolor font:Font_6(16) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:navTit];

        navTit.text= @"修改支付密码";
    
    
    [self addUIView];
    // Do any additional setup after loading the view.
}
-(void)addUIView{
    
    TKLlable * signLab = [TKLlable labelWithFrame:CGRectMake(20, kTopHeight+50, kWidth-40, 40) text:@"请设置新的支付密码" textColor:Bcolor font:Font_6(18) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:signLab];
    
   
    
    
    self.payWordF = [TKLtextFiled FiledWithFrame:CGRectMake(20, CGRectGetMaxY(signLab.frame)+25, kWidth-40, 40) text:@"  请输入新6位数支付密码" textColor:Bcolor font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor];
    self.payWordF.keyboardType = UIKeyboardTypeNumberPad;
    self.payWordF.delegate = self;
    [self.payWordF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    [self.view addSubview:self.payWordF];
    UIView * pLinView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.payWordF.frame), kWidth-40, 1)];
    pLinView.backgroundColor = JhColor(204, 204, 204);
    [self.view addSubview:pLinView];
    
    self.payWordMessageLab = [TKLlable labelWithFrame:CGRectMake(20, CGRectGetMaxY(signLab.frame)+15, kWidth-60, 14) text:@"rrrrrr" textColor:[UIColor redColor] font:Font_6(12) textAlignment:Textleft backgroundColor:Ccolor];
    self.payWordMessageLab.hidden = YES;
    [self.view addSubview:self.payWordMessageLab];
    
    //密文切换
    
    self.isHiddenBtn = [UIButton createBtnFrame:CGRectMake(kWidth-60, CGRectGetMaxY(signLab.frame)+30, 30, 20) title:@"" bgImageName:@"xianshi" action:^(UIButton *button) {

        [self ishiddenAction];
        
    }];
    self.isHiddenBtn.selected = YES;
    [self.isHiddenBtn setTitleColor:Bcolor forState:UIControlStateNormal];
    [self.view addSubview:self.isHiddenBtn];
    
    
    self.rePayWordMessageLab = [TKLlable labelWithFrame:CGRectMake(20, CGRectGetMaxY(pLinView.frame)+15, kWidth-60, 14) text:@"rrrrrr" textColor:[UIColor redColor] font:Font_6(12) textAlignment:Textleft backgroundColor:Ccolor];
    self.rePayWordMessageLab.hidden = YES;
    [self.view addSubview:self.rePayWordMessageLab];
    
    self.rePayWordF = [TKLtextFiled FiledWithFrame:CGRectMake(20, CGRectGetMaxY(self.payWordF.frame)+20, kWidth-40, 40) text:@"  再次输入6位数支付密码" textColor:Bcolor font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:self.rePayWordF];
    self.rePayWordF.delegate = self;
    [self.rePayWordF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.rePayWordF.keyboardType = UIKeyboardTypeNumberPad;
    UIView * rLinView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.rePayWordF.frame), kWidth-40, 1)];
    rLinView.backgroundColor = JhColor(204, 204, 204);
    [self.view addSubview:rLinView];
    
    UIButton * okBtn = [UIButton createBtnFrame:CGRectMake(20, CGRectGetMaxY(self.rePayWordF.frame)+30, kWidth-40, 40) title:@"确定" bgImageName:@"" action:^(UIButton *button) {
        [self.view endEditing:YES];
        self.payWordF.text = [self.payWordF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        self.rePayWordF.text = [self.rePayWordF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        
        if (self.payWordF.text.length == 6) {
            if (self.rePayWordF.text.length == 6) {
                if ([self.payWordF.text isEqualToString:self.rePayWordF.text]) {
                    //修改支付密码
                    self.payWordMessageLab.hidden = YES;
                    self.rePayWordMessageLab.hidden = YES;
                    [self changePayWordAction];
                }else{
                    self.rePayWordMessageLab.hidden = NO;
                    self.rePayWordMessageLab.text = @"两次密码输入不一致";
                }
            }else{
                
                self.rePayWordMessageLab.hidden = NO;
                self.rePayWordMessageLab.text = @"请输入6位数支付密码";
            }
        }else{
            
            self.payWordMessageLab.hidden = NO;
            self.payWordMessageLab.text = @"请输入6位数支付密码";
        }
        
     
        

    }];
    
    okBtn.backgroundColor = JhColor(75, 149,251);
    okBtn.layer.masksToBounds = YES;
    okBtn.layer.cornerRadius = 20;
    [self.view addSubview:okBtn];
    self.okBtn.userInteractionEnabled = NO;
    
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
  
    if (textField == self.payWordF) {
       
        self.payWordMessageLab.hidden = YES;
      
    }else{
        
     
        self.rePayWordMessageLab.hidden = YES;
    }
  
    
}
//监听长度
- (void)textFieldDidChange:(UITextField *)textField{
   
    if (textField == self.payWordF) {
        if (textField.text.length > 6) {
            self.payWordF.text = [textField.text substringToIndex:6];
           
        }
        if (textField.text.length == 0) {
            self.payWordMessageLab.hidden = YES;
        }
    }
    if (textField == self.rePayWordF) {
        if (textField.text.length > 6) {
            self.rePayWordF.text = [textField.text substringToIndex:6];
           
        }
        if (textField.text.length == 0) {
            self.rePayWordMessageLab.hidden = YES;
        }
    }
  
    
    
}


-(void)ishiddenAction{
    // 取反
    if (self.isHiddenBtn.selected == NO) {
        self.isHiddenBtn.selected = YES;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"xianshi"] forState:UIControlStateNormal];
    }else{
        self.isHiddenBtn.selected = NO;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"yincang"] forState:UIControlStateNormal];
    }
    self.payWordF.secureTextEntry = !self.payWordF.secureTextEntry;
    self.rePayWordF.secureTextEntry = !self.rePayWordF.secureTextEntry;
// 获得输入的值

    NSString *content = self.payWordF.text;
    NSString *recontent = self.rePayWordF.text;
 // 解决输入框从密文切换明文时出现空格的问题

    self.payWordF.text = @"";
    self.rePayWordF.text = @"";

    self.payWordF.text = content;
    self.rePayWordF.text = recontent;
 //如果是密文转明文进行二次编辑不会出现清空现象,如果是明文转密文进行二次编辑就会出现清空现象,进行判断,如果切换后此时输入框是密文状态,就保存原有内容并赋值给输入框,这样就不会出现清空现象了

   if (self.payWordF.secureTextEntry) {

 // 解决输入框从明文切换为密文时进行二次编辑出现清空现象

       [self.payWordF insertText:content];
       [self.rePayWordF insertText:recontent];

 }
 
    
    
}
-(void)changePayWordAction{
    //如果有旧密码-修改  否则重新签约下载证书
    if (self.oldPayWord.length > 0) {
        [self.user.cert changePINWithOldPIN:self.oldPayWord andNewPIN:self.payWordF.text andCompleteBlock:^(int resultCode, int remaintimes) {
            if (resultCode == IM_ER_SUCCESS) {
                [MBProgressHUD showMessage:@"修改成功" toView:self.view];
                
                [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
        
                
                
                
            }else{
              
                if(resultCode == IM_ER_USER_HAS_BEEN_LOCKED){
                    
                    [MBProgressHUD showMessage:@"账户锁定，请联系管理员" toView:self.view];
                }else{
                    NSLog(@"密码修改失败,%@",[IMError getMsgWithErr:resultCode]);
                }
            }
        }];
    }else{
        //
        [self.user.cert applyCertWithPIN:self.payWordF.text completeBlock:^(int resultCode) {
            if (resultCode == IM_ER_SUCCESS) {
                [MBProgressHUD showMessage:@"修改成功" toView:self.view];
                
                [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
              
                
                
                
            }else{
              
                if(resultCode == IM_ER_USER_HAS_BEEN_LOCKED){
                    
                    [MBProgressHUD showMessage:@"账户锁定，请联系管理员" toView:self.view];
                }else{
                    NSLog(@"密码修改失败,%@",[IMError getMsgWithErr:resultCode]);
                }
            }
        }];
        
       
        
    }
    

    
   
    
}
-(void)delayMethod{
    
    
   
   
        UIViewController *rootVC = self.presentingViewController;
        while(rootVC.presentingViewController) {
        rootVC= rootVC.presentingViewController;
        }
        
     [rootVC dismissViewControllerAnimated:YES completion:nil];
  
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
